clear all
set maxvar 30000
version 14
capture log close
set more off

****************************************************************************************************
* -----   Customize the paths and options:   ----- 
****************************************************************************************************
*cd  "Data\"
global MY_IN_PATH   "C:\Users\benjamin.balsmeier\Dropbox\Research\REStat\data"
global MY_OUT_PATH  "C:\Users\benjamin.balsmeier\Dropbox\Research\REStat"
*global MY_TEMP_PATH "..."

global MY_OUT_FILE  ${MY_OUT_PATH}out.dta
global MY_LOG_FILE  ${MY_OUT_PATH}cr_t9.log

log using "${MY_LOG_FILE}", text replace
****************************************************************************************************
* import CS data
*************************************************************************************************

use ${MY_IN_PATH}/Compustat.dta, clear

* merge CPI
merge n:1 year using ${MY_IN_PATH}/cpi1913_2017.dta, keep(3) nogen
replace cpi = cpi/100

gen lemp = log(emp+1)
gen lsale_def = log(sale*1000/cpi)
gen lrd_def = log(xrd/cpi)
gen lppe_def = log(ppent/cpi)

** Merge NBER shipment data, 4 digit 
destring sic, replace 
merge n:1 sic year using ${MY_IN_PATH}/output.dta, keep(3) nogen

gen output =  log(lout_d) 

* merge tech prox measure 
destring gvkey, replace
merge 1:1 gvkey year using ${MY_IN_PATH}/is.dta, 
drop if _merge == 2
drop _merge

gen tp5y = 1-tp_raw5_2020 
gen lnpat = log(npat2020+1)
gen sale_def = sale/cpi
gen ppe_def = ppent/cpi

xtset gvkey year
foreach var of varlist sale_def emp ppe_def {
gen f1`var' = f.`var'
}
foreach var of varlist sale_def emp ppe_def {
gen f2`var' = f2.`var'
}
foreach var of varlist sale_def emp ppe_def {
gen f3`var' = f3.`var'
}
foreach num of numlist 1/3 {
gen lglabprod`num' = log(f`num'sale_def/f`num'emp)
gen lgcapprod`num' = log(f`num'sale_def/f`num'ppe_def)
}
foreach var of varlist lglabprod1-lgcapprod3{
foreach num of numlist 1959/2015{
gen h`var' = `var' if year == `num'
qui sum h`var',d
replace `var' = r(p99) if `var' > r(p99) & h`var' !=.
replace `var' = r(p1) if `var' < r(p1) & h`var' !=.
drop h`var'
dis `num'
}
}

foreach num of numlist 1/10 {
gen t`num' = 0
}
replace t1 = 1 if year == 1962 | year == 1971 | year == 1976 | year == 1983 | year == 1992 | year == 2002 | year == 2010
replace t2 = 1 if year == 1963 | year == 1972 | year == 1977 | year == 1984 | year == 1993 | year == 2003 | year == 2011
replace t3 = 1 if year == 1964 				  | year == 1978 | year == 1985 | year == 1994 | year == 2004 | year == 2012
replace t4 = 1 if year == 1965 			      | year == 1979 | year == 1986 | year == 1995 | year == 2005 | year == 2013
replace t5 = 1 if year == 1966    			  | year == 1987 | year == 1996 | year == 2006 | year == 2014
replace t6 = 1 if year == 1967				  | year == 1988 | year == 1997				   | year == 2015
replace t7 = 1 if year == 1968				  | year == 1989 | year == 1998				   | year == 2016
replace t8 = 1 if 				 							   year == 1999
replace t9 = 1 if										       year == 2000
replace t1 = 1 if year == 1959 
gen t0 =0
replace t0 = 1 if  year == 1927 | year == 1933 |   year == 1938 |   year == 1945 |  year == 1949 | year == 1954 | year == 1958 | ///
				   year == 1961 | year == 1970 | year == 1975 | year == 1982 | year == 1991 | year == 2001 | year == 2009
gen tc = 0
forval i = 1(1)9 {
replace tc=`i' if t`i' == 1
}

replace tc = tc+1 if tc !=0
replace tc = 1 if t0 == 1
sum tc

gen boom1 = 0
foreach num of numlist 2/10 {
replace boom1 = 1 if t`num' ==1  	
}

drop if year < 1958
drop if year > 2011

egen h = count(1), by(gvkey)
drop if h < 2

foreach var of varlist boom1 {
gen `var'_x_tp5y = `var' * tp5y
}

xi: reghdfe lglabprod1 lrd_def tp5y boom1_*, absorb(year gvkey) cluster(sic)   
est store c
xi: reghdfe lglabprod2 lrd_def tp5y boom1_*, absorb(year gvkey) cluster(sic)  
est store d
xi: reghdfe lglabprod3 lrd_def tp5y boom1_*, absorb(year gvkey) cluster(sic)   
est store e
xi: reghdfe lgcapprod1 lrd_def tp5y boom1_*, absorb(year gvkey) cluster(sic)   
est store f
xi: reghdfe lgcapprod2 lrd_def tp5y boom1_*, absorb(year gvkey) cluster(sic)  
est store g
xi: reghdfe lgcapprod3 lrd_def tp5y boom1_*, absorb(year gvkey) cluster(sic) 
est store h
					
esttab c d e f g h,	starlevels(* 0.10 ** 0.05 *** 0.01) stats(N r2) ///
					keep (lrd_def tp5y boom1_*) ///
					order (lrd_def tp5y boom1_*) title("") compress label	
		

